
var api = require('../../../utils/api.js');
var $ = require('../../../utils/util.js');
var regeneratorRuntime = require('../../../utils/runtime.js')
var app = getApp();
var Store = app.Store


Page({
  data: {
    avatar: '', // 图片url
    isShowTime: false, // 是否显示时间弹框
    isShowPicker: false, // 是否显示选择项
    key_picker: '', // piker需要的字段
    imgUrl: api.url, // 图片url

    user_name: '',
    phonenumber: '',
    avatar: '', // 头像
    u_avatar:'',
    pinkIndex: 0, // 
    sexArr: [{ text: '男', type: 0 }, { text: '女', type: 1 }, { text: '未知', type: 3 }], // 
    sex:'', // 性别
  },
  onLoad: function (options) {
    this.setData({
     
      user_name: wx.getStorageSync('user_name'),
      phonenumber: wx.getStorageSync('phonenumber'),
      avatar: wx.getStorageSync('avatar'), // 头像
      sex: wx.getStorageSync('sex'), // 性别
    })
  },
  onShow(){
    if (this.data.u_avatar !== '') {
      $.uploadFile(api.main.upload, this.data.u_avatar, 'image').then((res) => {
        this.setData({
          avatar: res
        }) 
      })
    } 
  },

  uploadImgHandle() { //上传图片
    this.setData({
      u_avatar: '', 
    })
    wx.chooseImage({
      count: 1, // 默认9
      sizeType: ['compressed'], // 可以指定是原图还是压缩图，默认二者都有
      sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
      success(res) {
        const src = res.tempFilePaths[0]
        wx.navigateTo({
          url: `/pages/main/watermark/watermark?src=${src}&type=u_avatar`
        })
      }
    })
  },

  //##input 输入##
  input_handle(e) {
    this.setData({
      [e.currentTarget.dataset.key]: e.detail
    })
  },
  //##input 输入##
  sexHandle() { // 选择设备类型
    this.setData({
      pinkColumns: this.data.sexArr,
      pinkIndex: this.data.sexIndex,
      key_picker: 'sex',
      isShowPicker: true
    })
  },
  pickerConfirmHandle(e) { // 时间组件传出来的数据
    console.log(e.detail, 'xxxxxxxxxxx')
    var { value, key, index } = e.detail
    this.setData({
      [key]: value.type
    })  
  },
  updateMsgHandle(){ // 更新
    var params = {
      sex: this.data.sex,
      avatar: this.data.avatar,
    }
    api.$post(api.me.update, params).then((res) => { 
      wx.setStorageSync('avatar', res.data.avatar) 
      wx.setStorageSync('sex', res.data.sex) 
      $.toast('更新成功')
      setTimeout(() => {
        $.linkurl('', 'back')
      }, 1500)
    })
  }
})